//
//  KCompany_PersonalViewController.m
//  fxxiaoke
//
//  Created by 鞠超 on 2016/10/25.
//  Copyright © 2016年 jc. All rights reserved.
//

#import "KCompany_PersonalViewController.h"

#import "KCompany_PersonalListViewController.h"

@interface KCompany_PersonalViewController () <UITableViewDelegate, UITableViewDataSource, UISearchControllerDelegate, UISearchResultsUpdating>

@property (nonatomic, strong) UITableView * tableView;

@property (nonatomic, strong) UIView * view_head;
@property (nonatomic, strong) UISearchController * searchController;

@end

@implementation KCompany_PersonalViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self p_navi];
    
    [self p_setupView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - navi
- (void )p_navi
{
    _btnLeft.hidden = YES;
    _lblLeft.hidden = YES;
    _btnRight.hidden = YES;
    _lblRight.hidden = YES;
    
    _lblTitle.text = @"个人文件";
    _topView.backgroundColor = navi_bar_bg_color;
    
    //左返回
    UIImageView * image_LeftRetrun = [[UIImageView alloc] initWithFrame:CGRectMake(11, 31.5, 25, 25)];
    image_LeftRetrun.userInteractionEnabled = YES;
    image_LeftRetrun.image = [UIImage imageNamed:@"Left-return.png"];
    [_topView addSubview:image_LeftRetrun];
    
    UILabel * lbl_return = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(image_LeftRetrun.frame) - 6, 35, 65, 15)];
    lbl_return.text = @"XX网盘";
    lbl_return.textColor = [UIColor whiteColor];
    lbl_return.font = [UIFont systemFontOfSize:17];
    [_topView addSubview:lbl_return];
    
    UIButton * btn_return = [UIButton buttonWithType:(UIButtonTypeSystem)];
    btn_return.frame = CGRectMake(10, 22, 90, 40);
    [_topView addSubview:btn_return];
    [btn_return addTarget:self action:@selector(btn_returnAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    UIImageView * image_Setting = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 30, 32, 20, 20)];
    image_Setting.image = [UIImage imageNamed:@"Add_white.png"];
    [_topView addSubview:image_Setting];
    
    UIButton * btn_Setting = [UIButton buttonWithType:(UIButtonTypeSystem)];
    btn_Setting.frame = CGRectMake(SCREEN_WIDTH - 10 - 30, 24.5, 30, 35);
    [_topView addSubview:btn_Setting];
    [btn_Setting addTarget:self action:@selector(SettingAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    //
    UIImageView * image_list = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 35 - 30, 32, 20, 20)];
    image_list.image = [UIImage imageNamed:@"List_white.png"];
    [_topView addSubview:image_list];
    
    UIButton * btn_list = [UIButton buttonWithType:(UIButtonTypeSystem)];
    btn_list.frame = CGRectMake(SCREEN_WIDTH - 40 - 40, 24.5, 30, 35);
    [_topView addSubview:btn_list];
    [btn_list addTarget:self action:@selector(btn_listAction:) forControlEvents:(UIControlEventTouchUpInside)];
}

//左返回
- (void )btn_returnAction:(UIButton *)sender
{
    //取消活跃状态
    self.searchController.active = 0;
    
    [self dismissViewControllerAnimated:YES completion:^{
    }];
}

//添加文件夹名称
- (void)SettingAction:(UIButton *)sender
{
    NSLog(@"点击添加文件夹名称");
}

//列表
- (void)btn_listAction:(UIButton *)sender
{
//    NSLog(@"点击列表");
    
    KCompany_PersonalListViewController * kCompany_PersonalListViewController = [[KCompany_PersonalListViewController alloc] init];
    [self showViewController:kCompany_PersonalListViewController sender:nil];
}

//显示tabbar
- (void )viewWillAppear:(BOOL)animated
{
    [(AppDelegate *)[[UIApplication sharedApplication] delegate] hiddenTabBar];
}

#pragma mark - 布局
- (void )p_setupView
{
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64)];
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.tableFooterView = [[UIView alloc] init];
    [self p_headView];
    self.tableView.tableHeaderView = self.view_head;
    
    [self.view addSubview:self.tableView];
}

#pragma mark - tableview代理
- (NSInteger )numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell * cell = [[UITableViewCell alloc] init];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}

#pragma mark - 头视图
- (void )p_headView
{
    if(self.searchController.active == 0)
    {
        self.view_head = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 90)];
        self.view_head.backgroundColor = [UIColor groupTableViewBackgroundColor];
        self.tableView.tableHeaderView = self.view_head;
        
        //
        self.searchController.searchBar.frame = CGRectMake(0, 0, SCREEN_WIDTH, 44);
        self.searchController.searchBar.placeholder = @"搜索";
        self.searchController.dimsBackgroundDuringPresentation = NO;
        self.searchController.searchResultsUpdater = self;
        self.searchController.delegate = self;
        [self.view_head addSubview:self.searchController.searchBar];
        
        UILabel * lbl_title = [[UILabel alloc] initWithFrame:CGRectMake(15, 44 + 15, 100, 16)];
        lbl_title.text = @"个人文件";
        lbl_title.textColor = [UIColor grayColor];
        lbl_title.font = [UIFont systemFontOfSize:16.0f];
        [self.view_head addSubview:lbl_title];
    }
    else
    {
        self.view_head = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 44)];
        self.view_head.backgroundColor = [UIColor groupTableViewBackgroundColor];
        self.tableView.tableHeaderView = self.view_head;
        
        self.searchController.searchBar.frame = CGRectMake(0, 0, SCREEN_WIDTH, 44);
        self.searchController.searchBar.placeholder = @"搜索";
        self.searchController.dimsBackgroundDuringPresentation = NO;
        self.searchController.searchResultsUpdater = self;
        self.searchController.delegate = self;
        
        [self.view_head addSubview:self.searchController.searchBar];
    }
}

#pragma mark - UISearchController代理和update
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController
{
    if([searchController.searchBar.text length] > 0)
    {
        dispatch_async(dispatch_get_main_queue(), ^{
            //刷新tableView(记住,要更新放在主线程中)
            [self.tableView reloadData];
        });
    }
}

- (void)didPresentSearchController:(UISearchController *)searchController
{
    [self p_headView];
    
    dispatch_async(dispatch_get_main_queue(), ^{
        //刷新tableView(记住,要更新放在主线程中)
        [self.tableView reloadData];
    });
}

- (void)didDismissSearchController:(UISearchController *)searchController
{
    [self p_headView];
    
    dispatch_async(dispatch_get_main_queue(), ^{
        //刷新tableView(记住,要更新放在主线程中)
        [self.tableView reloadData];
    });
}


#pragma mark - 懒加载搜索框控制器
- (UISearchController *)searchController
{
    if(_searchController == nil)
    {
        self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
    }
    
    return _searchController;
}




















@end
